package es.deusto.eside.programacion4.mario2d;

import java.sql.SQLException;

import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Image;
import org.newdawn.slick.Music;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.openal.DeferredSound;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.util.Log;

import es.deusto.eside.programacion4.mario2d.Bd.database.GestorBD;
import es.deusto.eside.programacion4.mario2d.Bd.domain.Jugador;
import es.deusto.eside.programacion4.mario2d.states.FinalState;
import es.deusto.eside.programacion4.mario2d.states.FirstLevel;
import es.deusto.eside.programacion4.mario2d.states.GameState;
import es.deusto.eside.programacion4.mario2d.states.MainMenuState;
import es.deusto.eside.programacion4.mario2d.states.OptionStage;
import es.deusto.eside.programacion4.mario2d.states.Ranking;
import es.deusto.eside.programacion4.mario2d.states.RankingState;
import es.deusto.eside.programacion4.mario2d.states.SecondLevel;
import es.deusto.eside.programacion4.mario2d.states.ThirdLevel;
import es.deusto.eside.programacion4.mario2d.utils.PreferenceManager;

/**
 * Juego Mario
 * 
 * @author Sergio
 * 
 */
public class Mario2D extends StateBasedGame {

	/**
	 * Cancion menu
	 */
	public static Music menu;
	/**
	 * Cancion nivel
	 */
	public static Music level;
	/**
	 * cancion opciones
	 */
	public static Music option;
	/**
	 * Cancion monstruo final
	 */
	public static Music finalBattle;
	/**
	 * Musica ranking
	 */
	public static Music pole;
	/**
	 * WIDTH: ancho de la venta
	 */
	public static final int WIDTH = 640;
	/**
	 * HEIGHT: alto de la ventana
	 */
	public static final int HEIGHT = 480;
	/**
	 * Vidas
	 */
	public static int lifes = 3;
	/**
	 * Gestiona las opciones
	 */
	public static PreferenceManager p = new PreferenceManager();
	/**
	 * Jugador
	 */
	public static Jugador j;
	/**
	 * Base de datos
	 */
	public static GestorBD gBD;
	/**
	 * Indica si el juego ha finalizado
	 */
	public static boolean gameOver = false;
	/**
	 * Puntos
	 */
	public static int points = 0;

	/**
	 * Constructor
	 * 
	 * @param name
	 */
	public Mario2D(String name) {
		super(name);
	}

	/**
	 * Crea el juego
	 * 
	 * @param args
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */
	public static void main(String[] args) {
		Log.error("HOLA");
		try {
			menu = new Music("resources/Music/menu.ogg");
			level = new Music("resources/Music/level.ogg");
			pole = new Music("resources/Music/ranking.ogg");
			option = new Music("resources/Music/options.ogg");
			finalBattle = new Music("resources/Music/batallaFinal.ogg");
		} catch (SlickException e1) {
			e1.printStackTrace();
		}
		gBD = new GestorBD();
		try {
			gBD.connect();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		try {
			AppGameContainer app = new AppGameContainer(new Mario2D(
					"Mario Space"));
			app.setDisplayMode(WIDTH, HEIGHT, false);
			app.setFullscreen(false);
			app.setShowFPS(false);
			app.setVSync(true);
			app.start();
		} catch (SlickException e) {
			e.printStackTrace();
		}

	}

	@Override
	public void initStatesList(GameContainer arg0) throws SlickException {
		this.addState(new MainMenuState(GameState.MENU_STATE.ordinal()));
		this.addState(new FirstLevel(GameState.FIRST_LEVEL.ordinal()));
		this.addState(new SecondLevel(GameState.SECOND_LEVEL.ordinal()));
		this.addState(new ThirdLevel(GameState.THIRD_LEVEL.ordinal()));
		this.addState(new OptionStage(GameState.OPTION_STATE.ordinal()));
		this.addState(new FinalState(GameState.FINAL_LEVEL.ordinal()));
		this.addState(new RankingState(GameState.RANKING.ordinal()));
		this.addState(new Ranking(GameState.RANKING_MENU.ordinal()));

	}

}
